package comp
{
	use namespace comp_layout;

	import comp.Interface.IDispose;
	import comp.style.css.CssAchieve;
	import comp.style.css.MergeLayout;
	import comp.style.state.Skin;
	import comp.style.state.SkinState;
	import comp.style.state.TextColor;
	import comp.style.state.TextEnabled;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * <h1>按钮控件</h1>
	 * <h2>样式</h2>
	 * 支持的样式属性有</br>
	 * 文本:textRollOverColor移上的颜色，
	 * textSelectedColor选中的颜色,disabledColor不可用时字体颜色,
	 * ,fontFamily,fontSize,underline(false|true),color,textAlign("left"|"center"|"right")</br>
	 * 皮肤:skin默认皮肤,upSkin鼠标按起后的皮肤，overSkin鼠标在按钮上的皮肤,downSkin鼠标按下的皮肤,selectedSkin按钮为选中状态的皮肤.</br>
	 * 图标:icon为一个可视对像的实例</br>
	 * 内边距离:	iconRightMargin为icon与文本之间的间隔,paddingLeft=2左填充，paddingRight=4右填充,paddingTop=2上填充,paddingBottom=4下填充;
	 *
	 * </br>
	 * 示例:webgame.missionDaily.DailyView 95行</br>
	 * webgame.friends.Tips 83行
	 * @author:pu</br>
	 * date:2010-8-23 16:20
	 */
	public class Button extends UIComponent
	{
		include "style/BorderStyles";//包含样式
		include "style/TextStyles";//包含样式
		include "style/PaddingStyles"//内边距离
		include "style/MarginStyles"//外边距离
		include "style/Tips" //提示信息

		//*****
		//样式
		//******
		private var _textRollOverColor:uint;//=0xf8f8f8;//移上的颜色
		private var _textSelectedColor:uint;//=0xffffff;//选中的字体颜色
		private var _iconRightMargin:Number;//=0;//icon右边距离
		private var _iconTopMargin:Number;//=0;//icon右边距离

		private var _underline:Boolean;//=false;

		public function Button()
		{
			super();
			
			this.mouseChildren=false;
			
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			
			this._themeName="comp::Button";
			skinState=new SkinState(this);
		}
		
		public function get underline():Boolean
		{
			return _underline;
		}

		/**
		 * 下划线
		 */
		public function set underline(value:Boolean):void
		{
			_underline = value;
		}

		/**
		 * 设置按钮是否可用
		 */
		public override function set enabled(value:Boolean):void
		{
			_enabled = value;
			viewSkin("");
			if(this.enabled==false)
				onTipsMouseOut(null);
		}

		public function get iconRightMargin():Number
		{
			return _iconRightMargin;
		}

		/**
		 * 设置ICON与文字之间的间距
		 */
		public function set iconRightMargin(value:Number):void
		{
			_iconRightMargin = value;
		}
		
		public function get iconTopMargin():Number
		{
			return _iconTopMargin;
		}
		
		/**
		 * 设置ICON与文字之间的间距
		 */
		public function set iconTopMargin(value:Number):void
		{
			_iconTopMargin = value;
		}
		

		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * 设置按钮为选中状态
		 */
		public function set selected(value:Boolean):void
		{
			_selected = value;
			viewSkin("");
		}
		

		/**
		 *  Text color of the label as the user moves the mouse pointer over the button.
		 *  
		 *  @default 0x2B333C
		 */
		
		public function get textSelectedColor():uint
		{
			return _textSelectedColor;
		}
		
		public function set textSelectedColor(value:uint):void
		{
			_textSelectedColor = value;
		}
		
		
		/**
		 *  Text color of the label as the user presses it.
		 *  
		 *  @default 0x000000
		 */
		public function get textRollOverColor():uint
		{
			return _textRollOverColor;
		}
		
		public function set textRollOverColor(value:uint):void
		{
			_textRollOverColor = value;
		}
		
		
		//*****
		//皮肤
		//******
		private var _skin:Class;//", inherit="no", states="up, over, down, disabled, selectedUp, selectedOver, selectedDown, selectedDisabled")]
		
		/**
		 *  Name of the class to use as the default skin for the background and border. 
		 *  @default "mx.skins.halo.ButtonSkin"
		 */
		public function get skin():Class
		{
			return _skin;
		}
		
		/**
		 * @private
		 */
		public function set skin(value:Class):void
		{
			_skin = value;
		}
		
		private var _selectedSkin:Class;

		/**
		 * 选中的样式
		 */
		public function get selectedSkin():Class
		{
			return _selectedSkin;
		}

		public function set selectedSkin(value:Class):void
		{
			_selectedSkin = value;
		}
		
		
		private var _upSkin:Class;
		/**
		 *  Name of the class to use as the skin for the background and border
		 *  when the button is not selected and the mouse is not over the control.
		 *  
		 *  @default "mx.skins.halo.ButtonSkin"
		 */
		public function get upSkin():Class
		{
			return _upSkin;
		}

		/**
		 * @private
		 */
		public function set upSkin(value:Class):void
		{
			_upSkin = value;
		}

		
		private var _overSkin:Class;
		/**
		 *  Name of the class to use as the skin for the background and border
		 *  when the button is not selected and the mouse is over the control.
		 *  
		 *  @default "mx.skins.halo.ButtonSkin" 
		 */
		public function get overSkin():Class
		{
			return _overSkin;
		}

		/**
		 * @private
		 */
		public function set overSkin(value:Class):void
		{
			_overSkin = value;
		}
		
		private var _downSkin:Class;
		/**
		 *  Name of the class to use as the skin for the background and border
		 *  when the button is not selected and the mouse button is down.
		 *  
		 *  @default "mx.skins.halo.ButtonSkin"
		 */
		public function get downSkin():Class
		{
			return _downSkin;
		}

		/**
		 * @private
		 */
		public function set downSkin(value:Class):void
		{
			_downSkin = value;
		}
		
		private var _enabledSkin:Class;

		/**
		 * 
		 * 按钮不可用时候的皮肤
		 */
		public function get enabledSkin():Class
		{
			return _enabledSkin;
		}

		public function set enabledSkin(value:Class):void
		{
			_enabledSkin = value;
		}

		
		//*********
		//图标
		//*********
		protected var _icon:DisplayObject;

		/**
		 * button图标
		 */
		public function get icon():DisplayObject
		{
			return _icon;
		}

		public function set icon(value:DisplayObject):void
		{
			if(_icon && _icon.parent)
				_icon.parent.removeChild(_icon);
			_icon = value;
			if(this.initialized)
				merge();
		}
		
		public function get clickFunction():Function
		{
			return _clickFunction;
		}

		/**
		 * 点击事件函数
		 * 格式:
		 * function click(e:MouseEvent):void
		 */
		public function set clickFunction(value:Function):void
		{
			_clickFunction = value;
		}

		public function get text():String
		{
			return _text;;
		}
		
		public function set text(value:String):void
		{
			_text=value;
			
			if(initialized)
			{
				commitProperties();
				merge();
			}
		}
		
		override public  function dispose():void
		{
			super.dispose();
			removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			_currentSkin=null;
			_clickFunction=null;
			_textField=null;
			if(_icon is  IDispose)
				IDispose(_icon).dispose();
			while(this.numChildren>0)
				this.removeChildAt(0);
		}
		
		//protected function
		
		override protected function createChildren():void
		{
			_textField=new TextField;
			_textField.selectable=false;
			_textField.mouseEnabled=false;
			addChild(_textField);
			
			createTipsUI();
		}
		
		override protected function commitStyles():void
		{
			CssAchieve.mergeTextField(_textField,this);

			//注册状态
			skinState.defaultSkin=this.getStyle("skin");
			skinState.registState("",new Skin(getStyle("skin"),skinState.defaultSkin));
			skinState.registState("MOUSE_DOWN",new Skin(getStyle("downSkin"),skinState.defaultSkin));
			skinState.registState("MOUSE_UP",new Skin(getStyle("upSkin"),skinState.defaultSkin));
			skinState.registState("ROLL_OVER",new Skin(getStyle("overSkin"),skinState.defaultSkin));
			skinState.registState("ROLL_OUT",new Skin(getStyle("skin"),skinState.defaultSkin));
			skinState.registState("SELECTED",new Skin(getStyle("selectedSkin"),skinState.defaultSkin));
			skinState.registState("ENABLED",new Skin(getStyle("enabledSkin"),skinState.defaultSkin));
			
			var color:uint=this.getStyle("color");
			skinState.registState("SELECTED_TC",new TextColor(_textField,getStyle("textSelectedColor"),color));
			skinState.registState("ROLL_OVER_TC",new TextColor(_textField,getStyle("textRollOverColor"),color));
			skinState.registState("ROLL_OUT_TC",new TextColor(_textField,color));
			skinState.registState("_TC",new TextColor(_textField,color));
			skinState.registState("ENABLED_TC",new TextEnabled(_textField,getStyle("disabledColor")));

			if(_autoW)//		
				_textField.autoSize=this.getStyle("textAlign");	
		}
		
		override protected function commitProperties():void
		{
			if(_textField)
				_textField.htmlText=_text;
			
			commitTipsPropertis();
		}
		
		override protected function merge():void
		{
			var iconx:Number=0;
			var top:Number=0;
			top=getStyle("paddingTop");
			if(_icon is DisplayObject)
			{	
				addChild(_icon);//添加图标
				_icon.x=this.getStyleDefault("paddingLeft",0);
				_icon.y=top+ this.getStyleDefault("iconTopMargin",0);
				iconx=_icon.x+_icon.width+getStyleDefault("iconRightMargin",0);
			}
			
			MergeLayout.labelLayout(_textField,this,iconx);
			viewSkin(this._state);
			this.wholeRect=new Rectangle(0,0,this.width,this.height);
			
		}
		
		protected function mouseDownHandler(e:MouseEvent):void
		{
			if(!enabled)
				return;
			_state="MOUSE_DOWN";
			viewSkin(_state);	
		}
		
		protected function mouseUpHandler(e:MouseEvent):void
		{
			if(!enabled)
				return;
			_state="MOUSE_UP";
			viewSkin(_state);
			if(_clickFunction!=null)
				this._clickFunction(e);
		}
		
		protected function rollOverHandler(e:MouseEvent):void
		{
			onTipsMouseOver(e);

			if(!enabled)
				return;
			_state="ROLL_OVER";
			viewSkin(_state);
		}
		
		protected function rollOutHandler(e:MouseEvent):void
		{
			onTipsMouseOut(e);

			if(!enabled)
				return;
			_state="ROLL_OUT";
			viewSkin(_state);
		}

//		protected function viewEnabled():void
//		{
//			viewSkin("");
//		}

		/**
		 * 显示皮肤
		 */
		protected function viewSkin(stateName:String):void
		{
			if(this.enabled==false)
			{
				stateName="ENABLED";
				if(!filters)
					this.filters=sfilters;
				else
					this.filters.push(_colorFilter);
			}else
			{
				if(_colorFilter && filters)
				{
					var j:int=-1;
					if(filters==sfilters)
						filters=[];
					else
					{
						for (var i:int=0;i<filters.length;i++)
						{
							if(filters[i] instanceof ColorMatrixFilter)
								j=i;
						}
						this.filters=filters.splice(j,1);
					}
				
				}
				 if(this.selected)
					stateName="SELECTED";
			}
			
			
			this.skinState.changeState(stateName);
			this.skinState.doState(stateName+"_TC");
		}
		
		private var _text:String="";//显示文本内容
		protected var _state:String="";//按键状态
		protected var _textField:TextField;//按钮默认文本 
		private var _clickFunction:Function=null;
		private var _offx:Number=-2;
		private var _offy:Number=-3;
		public static var _colorFilter:ColorMatrixFilter=new ColorMatrixFilter([0.3086, 0.6094, 0.0820, 0, 0,  
			0.3086, 0.6094, 0.0820, 0, 0,  
			0.3086, 0.6094, 0.0820, 0, 0,  
			0,      0,      0,      1, 0]);

		public static var sfilters:Array=[_colorFilter];
//
		private var _selected:Boolean;//选中
		protected var _currentSkin:Object;//当前皮肤

		private var skinState:SkinState;
	}
}